package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.vo.SaveSkuVo;
import com.atguigu.gmall.starter.cache.service.CacheService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lhh
 * @ClassName SkuInfoController
 * @date 2022 2022/12/2
 */
@Api(tags = "sku管理接口")
@RestController
@RequestMapping("admin/product")
public class SkuInfoController {

    @Resource
    SkuInfoService skuInfoService;

    @Resource
    CacheService cacheService;

    @ApiOperation(value = "获取sku分页列表")
    @GetMapping("list/{page}/{limit}")
    public Result getSkuList(@PathVariable Integer page,
                             @PathVariable Integer limit){
        Page<SkuInfo> infoPage = skuInfoService.page(new Page<SkuInfo>(page, limit));
        return Result.ok(infoPage);
    }

    @ApiOperation(value = "添加sku")
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SaveSkuVo vo){
        skuInfoService.saveSkuInfo(vo);
        return Result.ok();
    }

    /**
     * 数据发生修改，进行延迟双删
     * @param vo
     * @return
     */
    @GetMapping("/updateSkuInfo")
    public Result updateSkuInfo(@RequestBody SaveSkuVo vo){

        cacheService.delayDoubleDel(RedisConst.SKU_DETAIL_CACHE+vo.getId());
        return Result.ok();
    }

    @ApiOperation(value = "上架")
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        skuInfoService.onSale(skuId);
        return Result.ok();
    }

    @ApiOperation(value = "下架")
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        skuInfoService.cancelSale(skuId);
        return Result.ok();
    }


}
